<?php
namespace app\admin\controller\management;

use app\admin\controller\Addon;
use think\facade\Request;
use app\common\controller\AdminController;
use app\admin\model\management\Spec as SpecModel;
use app\admin\model\management\SpecItem;
use think\App;
use think\facade\Db;
/**
 * @ControllerAnnotation(title="mall_spec")
 * NotParse
 */
class Spec extends AdminController
{    
    use \app\admin\traits\Curd;

    protected $sort = [
        // 
        // 'cate_id'   => 'desc',
  
        'sort' => 'asc',
    ];

    public function __construct(App $app)
    {
        parent::__construct($app);
        $this->model = new SpecModel();
        $this->SpecItem=new SpecItem();
    
      
    }
    public function index(){
        if ($this->request->isAjax()) {
            if (input('selectFields')) {
                return $this->selectList();
            }
            list($page, $limit, $where) = $this->buildTableParames();
            $count = $this->model->where($where)->count();
            $list  = $this->model->where($where)->order($this->sort)->page($page, $limit)->select();
            foreach ($list as $k => $v) {
                $content=$this->SpecItem->where("spec_id",$v['id'])->column('item');
                $list[$k]['spec']=implode(',',$content);
            }
      
            $data  = [
                'code'  => 0,
                'msg'   => '',
                'count' => $count,
                'data'  => $list,
            ];
            return json($data);
        }
        return $this->fetch();
    }
    public function add(){
        if ($this->request->isAjax()) {
            $post = $this->request->post();
             $rule = [
                // 'cate_id|cate_id'    => 'require',
                'content|规格值'     => 'require',
            ];
            $this->validate($post, $rule);
         
            try {
            
                $save = $this->model->save($post);
            } catch (\Exception $e) {
                $this->error('保存失败');
            }
            $save ? $this->success('保存成功') : $this->error('保存失败');
        }
       
        return $this->fetch();
    }
    public function edit()
    {
        $id  = Request::param('id');
        $row = $this->model->find($id);
        empty($row) && $this->error('数据不存在');
        if ($this->request->isAjax()) {
            $post = $this->request->post();
            $rule = [];
            $this->validate($post, $rule);
            try {
                $save = $row->save($post);
            } catch (\Exception $e) {
                $this->error('保存失败');
            }
            $save ? $this->success('保存成功') : $this->error('保存失败');
        }
        $content=$this->SpecItem->where("spec_id",$id)->column('item');
        $row->content=implode(',',$content);
        $this->assign('row', $row);
        return $this->fetch();
    }
     /**
     * @NodeAnotation(title="删除")
     */
    public function delete($id)
    {
        $row = $this->model->whereIn('id', $id)->select();
        $row->isEmpty() && $this->error('数据不存在');
        $itemId=$this->SpecItem->where("spec_id",$id)->column('id');
        try {
            $save = $row->delete();
        
        } catch (\Exception $e) {
            $this->error($e->getMessage());
        }
        $save ? $this->success('删除成功') : $this->error($e->getMessage());
    }
  
}
